<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <el-form-item label="房间">
        <el-select clearable filterable v-model="listQuery.pointNo" class="filter-item" placeholder="请选择上级分类">
          <el-option v-for="item in getFparentno" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button round size="medium" class="filter-item"
          :style="{background:buttonColor.edit,color:buttonColor.color}" @click="handleFilter">查找</el-button>
        <el-button round size="medium" class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}"
          @click="handleCreate">添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeight" v-loading="listLoading" :data="list" :header-cell-style="tableColor"
      stripe element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" :width="buttonColor.operationOne" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{color:buttonColor.delete,cursor:'pointer'}" @click="handleDelete(scope.row)">
              <img src="../../../public/images/del.png" />
              删除
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column width="150" :formatter="formatFparentno" align="left" label="房间" prop="pointNo" />
      <el-table-column width="120" align="left" label="姓名" prop="visitorUser" />
      <el-table-column width="220" align="left" label="公司" prop="companyName" />
      <el-table-column width="150" align="left" label="职称" prop="jobTitle" />
      <el-table-column width="120" align="left" label="手机号" prop="visitorPhone" />
      <el-table-column width="210" align="left" label="邮箱" prop="visitorEmail" />
      <el-table-column align="left" label="备注" prop="notes" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="dialogFormVisible" width="auto">
      <template slot="title">
        <div :style="titleColor">{{textMap[dialogStatus]}}</div>
      </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="80px"
        style="width: 400px; margin: 0 30px">
        <el-form-item class="required" label="房间" prop="pointNo">
          <el-select style="width:100%" clearable filterable v-model="dataForm.pointNo" class="filter-item"
            placeholder="请选择房间">
            <el-option v-for="item in getFparentno" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item class="required" label="姓名" prop="visitorUser">
          <el-input v-model="dataForm.visitorUser" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="dataForm.companyName" />
        </el-form-item>
        <el-form-item label="职称" prop="jobTitle">
          <el-input v-model="dataForm.jobTitle" />
        </el-form-item>
        <el-form-item label="手机号" prop="visitorPhone">
          <el-input v-model="dataForm.visitorPhone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="visitorEmail">
          <el-input v-model="dataForm.visitorEmail" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="dataForm.notes" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{background:buttonColor.cancel,color:buttonColor.colorone}"
          @click="dialogFormVisible = false">取消</el-button>
        <el-button round :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.button-margin {
  margin: 2px 0px;
}

.el-button+.el-button {
  margin-left: 1px;
}
</style>

<script>
import { listRoom, getVisitor, addVisitor, removeVisitor } from "@/api/visitors";
import {
  createequipmentClass,
  updateequipmentClass,
  deleteequipmentClass,
} from "@/api/equipmentClass";
import { getListArea } from '@/api/area'
import Pagination from "@/components/Pagination";
import moment from "moment";
import { tableColor, titleColor, buttonColor } from "@/filters/index";
export default {
  name: "Visitors",
  components: { Pagination },
  data() {
    return {
      buttonColor: buttonColor,
      getFparentno: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        pointNo: undefined,
      },
      dataForm: {
        companyName: undefined,
        jobTitle: undefined,
        pointNo: undefined,
        notes: undefined,
        visitorEmail: undefined,
        visitorPhone: undefined,
        visitorUser: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      tableColor: tableColor,
      titleColor: titleColor,
      rules: {
        pointNo: [{ required: true, message: "房间不能为空" }],
        visitorUser: [{ required: true, message: "姓名不能为空" }],
        visitorPhone: [
          {
            type: "number",
            message: "手机号必须为数字值",
            transform: (value) => Number(value),
          },
        ],
      },
    };
  },
  created() {
    // 房间列表
    listRoom()
      .then(response => {
        this.getFparentno = response.data.data.list
        // 默认房间
        this.listQuery.pointNo = this.getFparentno[0].id
        // 访客列表
        this.getList();
      })
      .catch(() => {
        this.getFparentno = []
      })
  },
  methods: {
    // 回显
    formatFparentno: function (row, column) {
      if (this.getFparentno) {
        for (let i = 0; i < this.getFparentno.length; i++) {
          if (row.pointNo == this.getFparentno[i].id) {
            return this.getFparentno[i].name
          }
        }
      } else {
        return ''
      }
    },
    // 日期 回显
    datetimeFormat(row, column) {
      const date = row[column.property];
      if (date === undefined || date === null) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    // 获取列表数据
    getList() {
      this.listLoading = true;
      getVisitor(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    // 查找
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 表单重置
    resetForm() {
      this.dataForm = {
        companyName: undefined,
        jobTitle: undefined,
        pointNo: undefined,
        notes: undefined,
        visitorEmail: undefined,
        visitorPhone: undefined,
        visitorUser: undefined,
      };
    },
    // 打开添加弹窗
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      if (this.getFparentno.length > 0) {
        this.dataForm.pointNo = this.getFparentno[0].id
      }
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    // 添加数据
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addVisitor(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加设备分类成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    // 打开更新弹窗
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    // 更新数据
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id
          this.dataForm.fupddate = new Date()
          updateequipmentClass(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fclassstno === this.dataForm.fclassstno) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新设备分类成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("是否要删除此访客", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeVisitor(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除访客成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response,
            });
          });
      });
    },
    // 序号
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>
